home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / ftpser1a / ftp_srv.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-10-04  |  13.0 KB  |  370 lines

  1. VERSION 5.00
  2. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.3#0"; "COMCTL32.OCX"
  3. Begin VB.Form FtpServ 
  4.    Appearance      =   0  'Flat
  5.    AutoRedraw      =   -1  'True
  6.    BackColor       =   &H00C0C0C0&
  7.    BorderStyle     =   1  'Fixed Single
  8.    Caption         =   "FTP SERVER"
  9.    ClientHeight    =   4575
  10.    ClientLeft      =   1455
  11.    ClientTop       =   3105
  12.    ClientWidth     =   8355
  13.    BeginProperty Font 
  14.       Name            =   "MS Sans Serif"
  15.       Size            =   8.25
  16.       Charset         =   0
  17.       Weight          =   700
  18.       Underline       =   0   'False
  19.       Italic          =   0   'False
  20.       Strikethrough   =   0   'False
  21.    EndProperty
  22.    ForeColor       =   &H80000008&
  23.    Icon            =   "FTP_SRV.frx":0000
  24.    LinkTopic       =   "FtpServ"
  25.    MaxButton       =   0   'False
  26.    MinButton       =   0   'False
  27.    PaletteMode     =   1  'UseZOrder
  28.    ScaleHeight     =   4575
  29.    ScaleWidth      =   8355
  30.    StartUpPosition =   2  'CenterScreen
  31.    Begin VB.TextBox UsrCnt 
  32.       Height          =   285
  33.       Left            =   3240
  34.       TabIndex        =   5
  35.       Text            =   "0"
  36.       Top             =   3960
  37.       Width           =   855
  38.    End
  39.    Begin VB.CommandButton EndCmd 
  40.       Caption         =   "Close Connection"
  41.       Height          =   375
  42.       Left            =   120
  43.       TabIndex        =   3
  44.       Top             =   3840
  45.       Width           =   1935
  46.    End
  47.    Begin VB.Frame StatFrame 
  48.       Caption         =   "Status Window"
  49.       Height          =   3735
  50.       Left            =   120
  51.       TabIndex        =   1
  52.       Top             =   0
  53.       Width           =   8055
  54.       Begin VB.ListBox LogWnd 
  55.          Appearance      =   0  'Flat
  56.          BackColor       =   &H00000000&
  57.          BeginProperty Font 
  58.             Name            =   "MS Serif"
  59.             Size            =   6.75
  60.             Charset         =   0
  61.             Weight          =   400
  62.             Underline       =   0   'False
  63.             Italic          =   0   'False
  64.             Strikethrough   =   0   'False
  65.          EndProperty
  66.          ForeColor       =   &H0000FF00&
  67.          Height          =   3165
  68.          ItemData        =   "FTP_SRV.frx":030A
  69.          Left            =   120
  70.          List            =   "FTP_SRV.frx":030C
  71.          TabIndex        =   2
  72.          Top             =   240
  73.          Width           =   7815
  74.       End
  75.    End
  76.    Begin ComctlLib.StatusBar StatusBar 
  77.       Align           =   2  'Align Bottom
  78.       Height          =   255
  79.       Left            =   0
  80.       TabIndex        =   0
  81.       Top             =   4320
  82.       Width           =   8355
  83.       _ExtentX        =   14737
  84.       _ExtentY        =   450
  85.       SimpleText      =   ""
  86.       _Version        =   327682
  87.       BeginProperty Panels {0713E89E-850A-101B-AFC0-4210102A8DA7} 
  88.          NumPanels       =   3
  89.          BeginProperty Panel1 {0713E89F-850A-101B-AFC0-4210102A8DA7} 
  90.             Object.Width           =   10654
  91.             MinWidth        =   10654
  92.             Object.Tag             =   ""
  93.          EndProperty
  94.          BeginProperty Panel2 {0713E89F-850A-101B-AFC0-4210102A8DA7} 
  95.             Style           =   6
  96.             Object.Width           =   2187
  97.             MinWidth        =   2187
  98.             TextSave        =   "10/05/1999"
  99.             Object.Tag             =   ""
  100.          EndProperty
  101.          BeginProperty Panel3 {0713E89F-850A-101B-AFC0-4210102A8DA7} 
  102.             Style           =   5
  103.             Object.Width           =   1764
  104.             MinWidth        =   1764
  105.             TextSave        =   "1:55 AM"
  106.             Object.Tag             =   ""
  107.          EndProperty
  108.       EndProperty
  109.    End
  110.    Begin VB.Timer Timer1 
  111.       Enabled         =   0   'False
  112.       Index           =   4
  113.       Interval        =   50
  114.       Left            =   7200
  115.       Top             =   3840
  116.    End
  117.    Begin VB.Timer Timer1 
  118.       Enabled         =   0   'False
  119.       Index           =   3
  120.       Interval        =   50
  121.       Left            =   6720
  122.       Top             =   3840
  123.    End
  124.    Begin VB.Timer Timer1 
  125.       Enabled         =   0   'False
  126.       Index           =   2
  127.       Interval        =   50
  128.       Left            =   6240
  129.       Top             =   3840
  130.    End
  131.    Begin VB.Timer Timer1 
  132.       Enabled         =   0   'False
  133.       Index           =   1
  134.       Interval        =   50
  135.       Left            =   5760
  136.       Top             =   3840
  137.    End
  138.    Begin VB.Timer Timer1 
  139.       Enabled         =   0   'False
  140.       Index           =   0
  141.       Interval        =   50
  142.       Left            =   5280
  143.       Top             =   3840
  144.    End
  145.    Begin VB.Label Label1 
  146.       Caption         =   "# of Users"
  147.       BeginProperty Font 
  148.          Name            =   "MS Sans Serif"
  149.          Size            =   8.25
  150.          Charset         =   0
  151.          Weight          =   400
  152.          Underline       =   0   'False
  153.          Italic          =   0   'False
  154.          Strikethrough   =   0   'False
  155.       EndProperty
  156.       Height          =   255
  157.       Left            =   2280
  158.       TabIndex        =   4
  159.       Top             =   3960
  160.       Width           =   975
  161.    End
  162.    Begin VB.Menu mSetup 
  163.       Caption         =   "Setup"
  164.    End
  165. Attribute VB_Name = "FtpServ"
  166. Attribute VB_GlobalNameSpace = False
  167. Attribute VB_Creatable = False
  168. Attribute VB_PredeclaredId = True
  169. Attribute VB_Exposed = False
  170. Private Sub EndCmd_Click()
  171. Dim i As Integer
  172.   For i = 1 To MAX_N_USERS    'close all connection
  173.     If users(i).control_slot <> INVALID_SOCKET Then
  174.       retf = closesocket(users(i).control_slot) 'close control slot
  175.     End If
  176.     If users(i).data_slot <> INVALID_SOCKET Then
  177.       retf = closesocket(users(i).data_slot) 'close data slot
  178.     End If
  179.   Next
  180.   retf = closesocket(ServerSlot)
  181.   If SaveProfile(App.Path & "\ftp_srv.ini", True) Then
  182.   End If
  183.   Unload Me
  184. End Sub
  185. Private Sub Form_Load()
  186. Dim i As Integer
  187. Dim hdr As String, item As String
  188.   '--- Initialization
  189.   'an FTP command is terminated by Carriage_Return & Line_Feed
  190.   'possible sintax errors in FTP commands
  191.   sintax_error_list(0) = "200 Command Ok."
  192.   sintax_error_list(1) = "202 Command not implemented, superfluous at this site."
  193.   sintax_error_list(2) = "500 Sintax error, command unrecognized."
  194.   sintax_error_list(3) = "501 Sintax error in parameters or arguments."
  195.   sintax_error_list(4) = "502 Command not implemented."
  196.   sintax_error_list(6) = "504 Command not implemented for that parameter."
  197.   'initializes the list which contains the names,
  198.   'passwords, access rights and default directory
  199.   'recognized by the server
  200.   If LoadProfile(App.Path & "\ftp_srv.ini") Then
  201.     '
  202.   Else
  203.     StatusBar.Panels(1) = "Error Loading Ini File!"
  204.   End If
  205.   'initializes the records which contain the
  206.   'informations on the connected users
  207.   For i = 1 To MAX_N_USERS
  208.     users(i).list_index = 0
  209.     users(i).control_slot = INVALID_SLOT
  210.     users(i).data_slot = INVALID_SLOT
  211.     users(i).IP_address = ""
  212.     users(i).Port = 0
  213.     users(i).data_representation = "A"
  214.     users(i).data_format_ctrls = "N"
  215.     users(i).data_structure = "F"
  216.     users(i).data_tx_mode = "S"
  217.     users(i).cur_dir = ""
  218.     users(i).state = 0
  219.     users(i).full = False
  220.   Next
  221.   OldWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
  222.   vbWSAStartup
  223.   'begins SERVER mode on port 21
  224.   ServerSlot = ListenForConnect(21, hWnd)
  225.   If ServerSlot > 0 Then
  226.     StatusBar.Panels(1) = Description
  227.   Else
  228.     StatusBar.Panels(1) = "Error Creating Listening Socket"
  229.   End If
  230. End Sub
  231. Private Sub Form_Unload(Cancel As Integer)
  232.   SetWindowLong hWnd, GWL_WNDPROC, OldWndProc
  233.   vbWSACleanup
  234. End Sub
  235. Private Sub mSetup_Click()
  236.   UserOpts.Show 1
  237. End Sub
  238. Private Sub Timer1_Timer(index As Integer)
  239. Dim close_data_cnt As Integer
  240. Dim error_on_data_cnt As Integer
  241. Select Case files_info(index).retr_stor
  242.   Case 0:  '--- R E T R  Command
  243.   If files_info(index).data_representation = "A" Then
  244.     If Not files_info(index).open_file Then
  245.       Open files_info(index).Full_Name For Input Lock Write As #index 'open file
  246.       files_info(index).open_file = True
  247.     End If
  248.     'sends the file on data connection; data are sent a line at a time
  249.     If files_info(index).try_again Then
  250.     Else      're-send old line
  251.       Line Input #index, files_info(index).Buffer
  252.     End If
  253.     retf = send_data(files_info(index).Buffer & vbCrLf, index)
  254.     If retf < 0 Then 'SOCKET_ERROR
  255.       retf = WSAGetLastError()
  256.       If retf = WSAEWOULDBLOCK Then
  257.         files_info(index).try_again = True
  258.       Else        'error on sending
  259.         error_on_data_cnt = True
  260.         close_data_cnt = True
  261.       End If
  262.     Else
  263.       files_info(index).try_again = False
  264.     End If
  265.     If EOF(index) Then close_data_cnt = True
  266.   Else  'binary transfer
  267.     If Not files_info(index).open_file Then
  268.       Open files_info(index).Full_Name For Binary Lock Write As #index
  269.       files_info(index).open_file = True
  270.     End If
  271.     'sends file on data connection; data are sent in blocks of 1024 bytes
  272.     If files_info(index).next_block = 0 Then
  273.       files_info(index).File_Len = LOF(index)
  274.       files_info(index).blocks = Int(files_info(index).File_Len / 1024)    '# of blocks
  275.       files_info(index).spare_bytes = files_info(index).File_Len Mod 1024  '# of remaining bytes
  276.       files_info(index).Buffer = String$(1024, " ")
  277.     End If
  278.     If files_info(index).next_block < files_info(index).blocks Then 'sends blocks
  279.       Get #index, files_info(index).next_byte + 1, files_info(index).Buffer
  280.       retf = send_data(files_info(index).Buffer, index)
  281.       If retf < 0 Then
  282.         retf = WSAGetLastError()
  283.         If retf = WSAEWOULDBLOCK Then  'try again
  284.         Else
  285.           error_on_data_cnt = True
  286.           close_data_cnt = True
  287.         End If
  288.       Else   'next block
  289.         files_info(index).next_block = files_info(index).next_block + 1
  290.         files_info(index).next_byte = files_info(index).next_byte + 1024
  291.       End If
  292.     Else    'sends remaining bytes
  293.       files_info(index).Buffer = String$(files_info(index).spare_bytes, " ")
  294.       Get #index, , files_info(index).Buffer
  295.       retf = send_data(files_info(index).Buffer, index)
  296.       close_data_cnt = True
  297.     End If
  298.   End If
  299.   If close_data_cnt Then  're-initialize files_info record
  300.     files_info(index).open_file = False
  301.     files_info(index).next_block = 0  'blocks count
  302.     files_info(index).next_byte = 0   'pointer to next block
  303.     files_info(index).try_again = False
  304.     Close #index    'close file
  305.     If error_on_data_cnt Then    'replies to user
  306.       retf = send_reply("550 RETR command not executed.", index)
  307.     Else
  308.       retf = send_reply("226 RETR command completed.", index)
  309.     End If
  310.     retf = close_data_connect(index)    'close data connection
  311.     Timer1(index).Enabled = False    'disables timer
  312.   End If
  313.   Case 1:  '--- S T O R  Command
  314.   If files_info(index).data_representation = "A" Then
  315.     If Not files_info(index).open_file Then    'open file
  316.       Open files_info(index).Full_Name For Output Lock Read Write As #index
  317.       files_info(index).open_file = True
  318.     End If
  319.     'receives file on data connection;  data are received a line at a time
  320.     retf = receive_data(files_info(index).Buffer, index)
  321.     If retf < 0 Then   'SOCKET_ERROR
  322.       retf = WSAGetLastError()
  323.       If retf = WSAEWOULDBLOCK Then   'try_again
  324.       Else       'error on receiving
  325.         error_on_data_cnt = True
  326.         close_data_cnt = True
  327.       End If
  328.     ElseIf retf = 0 Then  'connection closed by peer
  329.       close_data_cnt = True
  330.     Else 'retf > 0  write on file
  331.       Dummy$ = Left$(files_info(index).Buffer, retf)
  332.       Print #index, Dummy$
  333.     End If
  334.   Else  'binary transfer
  335.     If Not files_info(index).open_file Then   'open file
  336.       Open files_info(index).Full_Name For Binary Lock Read Write As #index
  337.       files_info(index).open_file = True
  338.     End If    'receives file on data connection;
  339.     retf = receive_data(files_info(index).Buffer, index)
  340.     If retf < 0 Then
  341.       retf = WSAGetLastError()
  342.       If retf = WSAEWOULDBLOCK Then  'try again
  343.       Else
  344.         error_on_data_cnt = True
  345.         close_data_cnt = True
  346.       End If
  347.     ElseIf retf = 0 Then     'connection closed by peer
  348.       close_data_cnt = True
  349.     Else
  350.       Dummy$ = Left$(files_info(index).Buffer, retf)
  351.       Put #index, , Dummy$
  352.     End If
  353.   End If
  354.   If close_data_cnt Then   're-initialize files_info record
  355.     files_info(index).open_file = False
  356.     files_info(index).next_block = 0 'blocks count
  357.     files_info(index).next_byte = 0  'pointer to next block
  358.     files_info(index).try_again = False
  359.     Close #index    'close file
  360.     If error_on_data_cnt Then    'replies to user
  361.       retf = send_reply("550 STOR command not executed.", index)
  362.     Else
  363.       retf = send_reply("226 STOR command completed.", index)
  364.     End If
  365.     retf = close_data_connect(index)     'closes data connection
  366.     Timer1(index).Enabled = False    'disables timer
  367.   End If
  368. End Select
  369. End Sub
  370.